package com.xxrl.shop.service;

import com.xxrl.shop.domain.ProductIndex;
import com.xxrl.shop.repository.ProductIndexRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author LinuY
 * @date 2020/7/31 0:46
 */
@Service
public class ProductIndexService {

    private final ElasticsearchRestTemplate elasticsearchRestTemplate;

    private final ProductIndexRepository productIndexRepository;

    public ProductIndexService(ElasticsearchRestTemplate elasticsearchRestTemplate, ProductIndexRepository productIndexRepository) {
        this.elasticsearchRestTemplate = elasticsearchRestTemplate;
        this.productIndexRepository = productIndexRepository;
    }

    public void insert(ProductIndex productIndex) {
        productIndexRepository.save(productIndex);
    }

    public void foo() {
        // 这玩意暂时还没学怎么用
        elasticsearchRestTemplate.get("1",ProductIndex.class);
    }

    public void insertList(List<ProductIndex> pks) {
        productIndexRepository.saveAll(pks);
    }

    public Iterable<ProductIndex> queryAllList() {
        return productIndexRepository.findAll();
    }

    public Page<ProductIndex> queryListByName(String productName, PageRequest pageRequest) {
        return productIndexRepository.findByProductNameLike(productName,pageRequest);
    }

    public void deleteById(Long id) {
        productIndexRepository.deleteById(id);
    }
}
